﻿namespace UFIDA.U9C.Cust.ZL.BP.DispatchBP
{
    using System;
    using System.Web;
    using System.Collections.Generic;
    using System.Text;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Util.Context;
    using UFIDA.U9.AP.APBill;
    using System.Linq;
    using UFSoft.UBF.Log;
    using UFIDA.U9.AP.ForAPI;
    using UFIDA.U9.Base;
    using UFSoft.UBF.PL.Engine.Cache;
    using UFIDA.U9.AP.Payment;
    using UFSoft.UBF.Transactions;

    /// <summary>
    /// AutoGeneratePRB partial 
    /// </summary>	
    public partial class AutoGeneratePRB
    {
        internal BaseStrategy Select()
        {
            return new AutoGeneratePRBImpementStrategy();
        }
    }

    #region  implement strategy	
    /// <summary>
    /// Impement Implement
    /// 
    /// </summary>	
    internal partial class AutoGeneratePRBImpementStrategy : BaseStrategy
    {
        public AutoGeneratePRBImpementStrategy() { }

        public override object Do(object obj)
        {
            //自动请款  调度
            AutoGeneratePRB bpObj = (AutoGeneratePRB)obj;
            try
            {
                if (System.Web.HttpRuntime.Cache["ZL.AutoPRB"] == null || (int.TryParse(System.Web.HttpRuntime.Cache["ZL.AutoPRB"].ToString(), out int innCount) && innCount == 0))
                {
                    System.Web.HttpRuntime.Cache["ZL.AutoPRB"] = 1;
                    var billHeads = APBillHead.Finder.FindAll("DocStatus=2 and IsSureAccrue=1").OfType<APBillHead>().Where(p => p.APBillLines.OfType<APBillLine>().Any(t => (t.Maturity - Context.LoginDate).Days <= 30));
                    if (billHeads.Any())
                    {

                        //UFIDA.U9.AP.PayReqFundBP.GetPrivateDescFieldByAPBillBP

                        //UFIDA.U9.AP.ForAPI.Proxy.CreatePayReqFundAPISVProxy proxy = new U9.AP.ForAPI.Proxy.CreatePayReqFundAPISVProxy();


                        //PayReqFundAPISV


                        foreach (var billhead in billHeads)
                        {
                            //using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                            //{
                                try
                                {
                                    //产生请款单
                                    AutoGeneratePRBLogic.GenPRB(billhead);
                                    //调用OA的流程

                                    //scope.Commit();
                                }

                                catch (Exception ex)

                                {
                                    //scope.Rollback();
                                    LogHelper.Log.Error("UFIDA.U9C.Cust.ZL.BP.DispatchBP.AutoGeneratePRB", ex);

                                }



                           // }

                        }

                    }

                    System.Web.HttpRuntime.Cache["ZL.AutoPRB"] = 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log.Error("UFIDA.U9C.Cust.ZL.BP.DispatchBP.AutoGeneratePRB", ex);
                System.Web.HttpRuntime.Cache["ZL.AutoPRB"] = 0;
            }

           

            return bpObj;
        }
    }

    #endregion


}